package com.inossem.warehousestorage_inossem.activity.inventory;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.inossem.utils.base.RegexUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.up_frame.UpFrameOverviewActivity;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryAreaBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryNewMatQueryReqBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryOrdinaryMatReqBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryOrdinaryMaterieBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryRespBean;
import com.inossem.warehousestorage_inossem.constant.RequestConstant;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inventory.InventoryManageRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.InventoryUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

public class InventoryItemActivity extends BaseActivity {

    private TextView title;

    private TextView
            total,
            materielCode,
            positionNumber,
            number,
            inventoryNumber,
            materielDescription,
            specialInventoryFlagValue,
            specialInventoryDescription,
            batch,
            specialInventoryCode,
            unit,
            nextItem,
            preItem;

    private int index = 0;

    //    private InventoryOverviewBean.DataBean overviewBean;
    private InventoryOrdinaryMaterieBean.DataBean bean;
    private List<InventoryOrdinaryMaterieBean.DataBean.MaterielListBean> items;
    private InventoryAreaBean.DataBean areaBean;
    private InventoryOrdinaryMaterieBean.DataBean.MaterielListBean item;
    private PercentRelativeLayout bottom,row6;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_inventory_item);
        initView(InventoryItemActivity.this);
        initData(InventoryItemActivity.this);
    }


    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        this.total = (TextView) activity.findViewById(R.id.total);
        this.preItem = (TextView) activity.findViewById(R.id.preItem);
        this.nextItem = (TextView) activity.findViewById(R.id.nextItem);

        this.materielCode = (TextView) activity.findViewById(R.id.materielCode);
        this.positionNumber = (TextView) activity.findViewById(R.id.positionNumber);
        this.number = (TextView) activity.findViewById(R.id.number);
        this.inventoryNumber = (TextView) activity.findViewById(R.id.inventoryNumber);
        this.materielDescription = (TextView) activity.findViewById(R.id.materielDescription);
        this.specialInventoryFlagValue = (TextView) activity.findViewById(R.id.specialInventoryFlagValue);
        this.specialInventoryDescription = (TextView) activity.findViewById(R.id.specialInventoryDescription);
        this.batch = (TextView) activity.findViewById(R.id.batch);
        this.specialInventoryCode = (TextView) activity.findViewById(R.id.specialInventoryCode);
        this.unit = (TextView) activity.findViewById(R.id.unit);

        this.bottom = (PercentRelativeLayout) activity.findViewById(R.id.bottom);
        this.row6 = (PercentRelativeLayout) activity.findViewById(R.id.row6);

    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_inventory_item_title));
        ActivityUtils.initBackAndFunction(activity, activity.getResources().getString(R.string.activity_inventory_overview_title), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {

                goBack();
            }
        });
        this.nextItem.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (!TextUtils.isEmpty(number.getText().toString())&&!RegexUtils.checkDecimal(number.getText().toString())) {
                    Utils.middleToast(activity,"实盘数量不合法");
                    return;
                }else{
                    InventoryItemActivity.this.index++;
                    load();
                }

            }
        });

        this.preItem.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (!TextUtils.isEmpty(number.getText().toString())&&!RegexUtils.checkDecimal(number.getText().toString())) {
                    Utils.middleToast(activity,"实盘数量不合法");
                    return;
                }else {
                    InventoryItemActivity.this.index--;
                    load();
                }
            }
        });

        this.number.addTextChangedListener(new TextWatcher(){
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                //不然每次输入，都会调用一次set方法
                if (getCurrentFocus() == number) {

                    item.setNumber(number.getText().toString());

                }

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        getData(activity);
    }

    private void goBack() {
        if (!TextUtils.isEmpty(number.getText().toString())&&!RegexUtils.checkDecimal(number.getText().toString())) {
            Utils.middleToast(this,"实盘数量不合法");
            return;
        }
        InventoryOrdinaryMatReqBean imrb = InventoryUtils.genUpdateItems(this.bean);
        if (imrb.getMaterielList().size() > 0) {

            InventoryManageRequest.updateItems(this, imrb, new GsonBeanListener<InventoryRespBean>() {
                @Override
                public void onSuccess(InventoryRespBean bean) {
                    if (bean.getData().getCode().equals(RequestConstant.BEAN_CODE_SUCCESS)) {
                        finish();
                    } else {
                        Utils.middleToast(InventoryItemActivity.this, bean.getData().getMsg());
                    }


                }
            });
        } else {
            DialogUtils.getConfirmDialog(this, "实盘数量没有填写，退出将不保存任何信息", new DialogUtils.ConfirmDialogListener() {
                @Override
                public void onConfirm(Dialog dialog) {
                    finish();
                }
            }).show();

        }

    }

    private void getData(Activity activity) {

        String areaNumber = activity.getIntent().getStringExtra(InventoryExtra.AREA_NUMBER_VALUE);
        String positionNumber = activity.getIntent().getStringExtra(InventoryExtra.POSITION_NUMBER_VALUE);
        areaBean = SPUtils.getInformation(activity, InventoryAreaBean.DataBean.class, SPUtils.KEY_INVENTORY_INFORMATION);

        InventoryNewMatQueryReqBean query = new InventoryNewMatQueryReqBean();
        query.setAreaNumberValue(areaNumber);
        query.setPositionNumberValue(positionNumber);
        query.setInventoryVoucher(areaBean.getInventoryVoucher());

        InventoryManageRequest.findOrdList(activity, query, new GsonBeanListener<InventoryOrdinaryMaterieBean>() {
            @Override
            public void onSuccess(InventoryOrdinaryMaterieBean bean) {
                items = bean.getData().getMaterielList();
                InventoryItemActivity.this.bean = bean.getData();
                load();
            }
        });


    }


    private void load() {
        if (items.size() <= 0) {
            Utils.middleToast(this, "数据加载异常");
            finish();
            return;
        }
        item = items.get(index);
        //判断是否显示库存数量，明盘显示
        if(InventoryUtils.isWhite(areaBean)){
            row6.setVisibility(View.VISIBLE);
        }

        InventoryItemActivity.this.materielCode.setText(item.getMaterielCode());
        InventoryItemActivity.this.positionNumber.setText(bean.getPosition());
        InventoryItemActivity.this.number.setText(item.getNumber());
        InventoryItemActivity.this.inventoryNumber.setText(item.getInventoryNumber() + item.getUnit());
        InventoryItemActivity.this.materielDescription.setText(item.getMaterielDescription());
        if(!TextUtils.isEmpty(item.getSpecialInventoryFlagCode() )){
            InventoryItemActivity.this.specialInventoryFlagValue.setText(item.getSpecialInventoryFlagCode() + "-" + item.getSpecialInventoryFlagValue());
        }
        InventoryItemActivity.this.specialInventoryDescription.setText(item.getSpecialInventoryDescription());
        InventoryItemActivity.this.batch.setText(item.getBatch());
        InventoryItemActivity.this.specialInventoryCode.setText(item.getSpecialInventoryCode());

        InventoryItemActivity.this.total.setText(index + 1 + "/" + items.size());
        unit.setText(item.getUnit());

        if (items.size() > 1) {
            this.bottom.setVisibility(View.VISIBLE);
            if (index == 0) {
                preItem.setVisibility(View.INVISIBLE);
                nextItem.setVisibility(View.VISIBLE);
            } else if (index == items.size() - 1) {
                nextItem.setVisibility(View.INVISIBLE);
                preItem.setVisibility(View.VISIBLE);
            } else {
                nextItem.setVisibility(View.VISIBLE);
                preItem.setVisibility(View.VISIBLE);
            }
        } else {
            this.bottom.setVisibility(View.GONE);
        }

    }


    @Override
    public void onBackPressed() {
        goBack();
    }
}
